package iscript.uiFramework.components.form 
{
	import flash.events.Event;
	import flash.events.TextEvent;
	import iscript.uiFramework.components.button.ImageButton;
	import iscript.uiFramework.components.UIComponent;
	import iscript.uiFramework.event.TextInputEvent;
	import iscript.uiFramework.UIFramework;
	
	
	/**
		 * @example
		 * 	var ni:NumberInput = new NumberInput()
			ni.x = 400;
			ni.y = 350;
			
			ni.minValue = 0
			ni.maxValue = 10
			ni.stepValue = 0.01
			ni.value = 2
			//ni.width=150
			ni.render(this);
		 */
	
	public class NumberInput extends UIComponent
	{
		
		
		private var _minValue:Number;
		private var _maxValue:Number;
		private var _stepValue:Number;
		private var _textInput:TextInput;
		private var _topButton:ImageButton;
		private var _bottomButton:ImageButton;
		private var _value:Number;
		private var _exponentialLen:int;
		
		

		public function NumberInput() 
		{
	 
			_preferWidth = 100;
			_minValue = 0;
			_maxValue = 10;
			_stepValue = 1;
			_value = _minValue;
			_exponentialLen = 0;
			super(_preferWidth);
		}
		
		
		override protected function initialize():void 
		{
			_textInput = new TextInput();
			_textInput.text = _minValue.toString();
			_textInput.restrict = "0-9 ."
			_textInput.width = _preferWidth;
			_textInput.render(this);
			_topButton = new ImageButton(UIFramework.resource.getBitmapData("GameLite.UIComponent." + this.theme + ".NmberInput.Top.Normal"),
										 UIFramework.resource.getBitmapData("GameLite.UIComponent." + this.theme + ".NmberInput.Top.Hover"),
										 UIFramework.resource.getBitmapData("GameLite.UIComponent." + this.theme + ".NmberInput.Top.Down")
										);
			_topButton.holdDownTime = 100;
			_topButton.x = _textInput.width;
			_topButton.render(this);
			
			_bottomButton = new ImageButton(UIFramework.resource.getBitmapData("GameLite.UIComponent." + this.theme + ".NmberInput.Bottom.Normal"),
										    UIFramework.resource.getBitmapData("GameLite.UIComponent." + this.theme + ".NmberInput.Bottom.Hover"),
										    UIFramework.resource.getBitmapData("GameLite.UIComponent." + this.theme + ".NmberInput.Bottom.Down")
										   );
			_bottomButton.holdDownTime = 100;
			_bottomButton.x = _textInput.width;
			_bottomButton.y = _topButton.height;
			_bottomButton.render(this);
			
			this.width = _preferWidth;
			
			super.initialize();
		}
		
		override protected function eventListener():void 
		{ 
			_textInput.addEventListener(TextInputEvent.TEXTINPUT,_textInputChangeLogic)
			_topButton.ClickHandler = _topButton.HoldDownHandler = function(e:*):void {
				   value = _value+_stepValue;
				}
				
			_bottomButton.ClickHandler  = _bottomButton.HoldDownHandler= function(e:*):void {
				   value = _value-_stepValue;
				}
			
			super.eventListener();
		}
		
		private function _textInputChangeLogic(e:TextInputEvent):void 
		{ 
			value = Number(e.text);
		}
		

		
		override public function dispose():void 
		{
			if(!Disposed){
			_textInput.removeEventListener(TextInputEvent.TEXTINPUT,_textInputChangeLogic)
			_topButton.ClickHandler = _topButton.HoldDownHandler = null;
			_bottomButton.ClickHandler  = _bottomButton.HoldDownHandler = null;
			super.dispose();
			}
		}
		
		override public function set width(value:Number):void 
		{
			if (value <= _topButton.width) value = _topButton.width+6;
			_textInput.width = value - _topButton.width;
			_topButton.x = _textInput.width;
			_bottomButton.x = _textInput.width;
			_bottomButton.y = _topButton.height;
		}
		
		public function get value():Number 
		{
			return Number(_textInput.text);
		}
		
		public function set value(value:Number):void 
		{
			if (value < this._minValue) value = _minValue;
			if (value > this._maxValue) value = _maxValue;
			if(_isFloatStep){
			var floatStr:String  = String(value.toPrecision(_maxValue.toString().length+_exponentialLen));
			_textInput.text = floatStr.substring(0, floatStr.indexOf(".") + 1 + _exponentialLen);			
			}else {
			_textInput.text = value.toString();	
			}
			_value = Number(_textInput.text);
		}
		
		public function get minValue():Number 
		{
			return _minValue;
		}
		
		public function set minValue(value:Number):void 
		{
			_minValue = value;
			//if (_minValue > _maxValue )_minValue=_maxValue;
		}
		
		public function get maxValue():Number 
		{
			return _maxValue;
		}
		
		public function set maxValue(value:Number):void 
		{
			_maxValue = value;
			//if (_maxValue < _minValue)_maxValue = _minValue;
		}
		
		public function get stepValue():Number 
		{
			return _stepValue;
		}
		
		private var _isFloatStep:Boolean;
		
		public function set stepValue(value:Number):void 
		{
			_stepValue = value;
			var index:int = _stepValue.toString().indexOf(".");
			if(index != -1){
				 _exponentialLen = _stepValue.toString().length-1 - index;
				 _isFloatStep = true;
			}else{
				 _isFloatStep = false;
			}
		}
		
		
		
	}

}